package com.bdmc.work.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.lock.annotation.Lock4j;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.work.domain.WorkOrder;
import com.bdmc.work.domain.bo.WorkOrderBo;
import com.bdmc.work.domain.vo.WorkOrderSumVo;
import com.bdmc.work.domain.vo.WorkOrderVo;
import com.bdmc.work.mapper.WorkOrderMapper;
import com.bdmc.work.service.IWorkOrderService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 清运任务工单Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-30
 */
@Service
public class WorkOrderServiceImpl extends ServicePlusImpl<WorkOrderMapper, WorkOrder, WorkOrderVo> implements IWorkOrderService {

    @Override
    public WorkOrderVo queryById(Long orderId){
        return getVoById(orderId);
    }

    @Override
    public TableDataInfo<WorkOrderVo> queryPageList(WorkOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WorkOrder> lqw = buildQueryWrapper(bo);
        Page<WorkOrderVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<WorkOrderVo> mySelectPage(WorkOrderBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public TableDataInfo<WorkOrderSumVo> sumSelectPage(WorkOrderBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.sumSelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<WorkOrderSumVo> sumSelectPage(WorkOrderBo bo) {
        return baseMapper.sumSelectPage(bo);
    }

    @Override
    public List<WorkOrderVo> queryList(WorkOrderBo bo) {
        return baseMapper.mySelectPage(bo);
    }

    private LambdaQueryWrapper<WorkOrder> buildQueryWrapper(WorkOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WorkOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, WorkOrder::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNum()), WorkOrder::getOrderNum, bo.getOrderNum());
        lqw.eq(bo.getOrderTime() != null, WorkOrder::getOrderTime, bo.getOrderTime());
        lqw.eq(bo.getFacilityId() != null, WorkOrder::getFacilityId, bo.getFacilityId());
        lqw.eq(bo.getDeviceId() != null, WorkOrder::getDeviceId, bo.getDeviceId());
        lqw.eq(bo.getOrderStatus() != null, WorkOrder::getOrderStatus, bo.getOrderStatus());
        lqw.eq(bo.getCarId() != null, WorkOrder::getCarId, bo.getCarId());
        lqw.eq(StringUtils.isNotBlank(bo.getCarNum()), WorkOrder::getCarNum, bo.getCarNum());
        lqw.eq(bo.getDriverId() != null, WorkOrder::getDriverId, bo.getDriverId());
        lqw.like(StringUtils.isNotBlank(bo.getDriverName()), WorkOrder::getDriverName, bo.getDriverName());
        lqw.eq(bo.getDriverDealStatus() != null, WorkOrder::getDriverDealStatus, bo.getDriverDealStatus());
        lqw.eq(bo.getDriverDealTime() != null, WorkOrder::getDriverDealTime, bo.getDriverDealTime());
        lqw.eq(bo.getManagerId() != null, WorkOrder::getManagerId, bo.getManagerId());
        lqw.like(StringUtils.isNotBlank(bo.getManagerName()), WorkOrder::getManagerName, bo.getManagerName());
        lqw.eq(bo.getCleanerId() != null, WorkOrder::getCleanerId, bo.getCleanerId());
        lqw.like(StringUtils.isNotBlank(bo.getCleanerName()), WorkOrder::getCleanerName, bo.getCleanerName());
        lqw.eq(bo.getCleanerDealStatus() != null, WorkOrder::getCleanerDealStatus, bo.getCleanerDealStatus());
        lqw.eq(bo.getCleanerDealTime() != null, WorkOrder::getCleanerDealTime, bo.getCleanerDealTime());
        return lqw;
    }


    @Override
    @Lock4j(keys = {"#bo.facilityId"})
    public  Boolean insertByBo(WorkOrderBo bo) {
        //判断是否已派单
        LambdaQueryWrapper<WorkOrder> w1 = Wrappers.lambdaQuery();
        w1.eq(WorkOrder::getFacilityId,bo.getFacilityId()).lt(WorkOrder::getOrderStatus,3);
        long count = count(w1);
        if(count>0)
            return false;
        WorkOrder add = BeanUtil.toBean(bo, WorkOrder.class);
        validEntityBeforeSave(add);
        long id = IdUtil.getSnowflake(1, 1).nextId();
        add.setOrderNum(id+"");
        boolean flag = save(add);
        if (flag) {
            bo.setOrderId(add.getOrderId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(WorkOrderBo bo) {
        WorkOrder update = BeanUtil.toBean(bo, WorkOrder.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(WorkOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
